<?php
/**
 * User: 魔十七
 * Date: 2020/11/10
 * Time: 13:30
 */

namespace Samecity\Controller;

use Think\Controller;
use Samecity\Model\ShopCartModel;

class ShopCartController extends Controller
{
    private $ShopCartModel;

    protected function _initialize()
    {
        //成员变量 ShopCartModel对象
        $this->ShopCartModel = new \Samecity\Model\ShopCartModel();
    }

    /**
     * 购物车列表
     * @param $openId   用户openID
     */
    public function ShopCartList()
    {
         $post = $_POST;
         $openId = $post['openId'];       // 用户OpenId
         $data = $this->ShopCartModel->getShopCartList($openId);
         $this->ajaxReturn($data);
    }

    /**
     * 添加购物车
     * @param   $open_id    用户openID
     * @param   $cart_type  暂时不用购物车类型
     * @param   $shop_id    商铺ID
     * @param   $goods_id   商品ID
     * @param   $spec       商品规格 (json)
     * @param   $nums       商品数量
     * @param   $type  数量类型 1是累加，2是覆盖
     */
    public function CartAdd()
    {
        $post = $_POST;
        $open_id    = isset($post['openid']) ? $post['openid'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'open_id异常'));
        $shop_id    = isset($post['shop_id']) ? $post['shop_id'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'shop_id异常'));
        $goods_id   = isset($post['goods_id']) ? $post['goods_id'] : $this->ajaxReturn(array('code'=>1002,'msg'=>'goods_id异常'));
        $spec       = isset($post['spec']) ? $post['goods_id'] : '{"msg":"默认尺寸"}';
        $nums       = isset($post['nums']) ? $post['nums'] : 1;
        $type       = isset($post['type']) ? $post['type'] : 1;

        $data = $this->ShopCartModel->getCartAdd($open_id,$shop_id,$goods_id,$spec,$nums,$type);
        $this->ajaxReturn($data);
    }

    /**
     * 购物车删除
     * @param $open_id           用户openID
     * @param $goods_id  string  商品ID (,分隔)
     */

    public function CartDelete()
    {
        $post = $_POST;
        $open_id = isset($post['open_id']) ? $post['open_id'] :$this->ajaxReturn(array('code'=>1002,'msg'=>'open_id异常'));
        $cart_id = isset($post['cart_id']) ? $post['cart_id'] :$this->ajaxReturn(array('code'=>1002,'msg'=>'cart_id异常'));

        $data = $this->ShopCartModel->getCartDelete($open_id,$cart_id);
        $this->ajaxReturn($data);
    }




    /*
     * 无用代码
     * public function getShopCartList($openid = 'o2FTP4luddGeIJa8xCv8_y4_I8R8')
    {
        $shop = M('shop_cart')->field('samecity_merchant.id,samecity_merchant.uid')
            ->join('samecity_merchant ON samecity_shop_cart.shop_id=samecity_merchant.id')
            ->distinct(true)->select();
        foreach($shop as $key => $value){
            $dianpu[$key] = $value['title'];
            // 购物车商品
            $where['samecity_shop_cart.openid'] = $openid;
            $where['samecity_goods.shop_id'] = $value['id'];

            foreach($dianpu as $keys => $values){
                $Model = M('shop_cart')
                    ->join('samecity_goods ON samecity_shop_cart.goods_id=samecity_goods.id')
                    ->where($where)
                    ->order('samecity_shop_cart.cart_id desc')
                    ->select();
            }
        }
        $data = array(
            'title' => $values,
            'item'  => $Model
        );
        return $data;
    }*/

}